package com.ccc.demoboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ccc.demoboot.common.R;
import com.ccc.demoboot.pojo.Code;
import com.ccc.demoboot.pojo.Material;
import com.ccc.demoboot.pojo.Role;
import com.ccc.demoboot.pojo.Warehouse;
import com.ccc.demoboot.service.WarehouseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author wz
 * @Date 2024/6/4 9:57
 * @PackageName:com.ccc.demoboot.controller
 * @ClassName: WarehouseController
 */
@RequestMapping("/warehouse")
@RestController
public class WarehouseController {
    @Autowired
    WarehouseService warehouseService;


    /**
     * 根据条件查询仓库
     *
     * @param warehouse
     * @return
     */
    @PostMapping("/select")
    public R select(@RequestBody Warehouse warehouse) {
        QueryWrapper<Warehouse> warehouseQueryWrapper = new QueryWrapper<>();
        warehouseQueryWrapper.like("warehouse", warehouse.getWarehouse());
        List<Warehouse> list = this.warehouseService.list(warehouseQueryWrapper);
        return new R().add("list", list);
    }

    /**
     * 根据id查询仓库
     * @param warehouse
     * @return
     */
    @PostMapping("/selectid")
    public R selectId(@RequestBody Warehouse warehouse) {

        QueryWrapper<Warehouse> warehouseQueryWrapper = new QueryWrapper<>();
        warehouseQueryWrapper.setEntity(warehouse);
        List<Warehouse> list = this.warehouseService.list(warehouseQueryWrapper);
        return new R().add("list", list);
    }

    /**
     * 获取仓库,区,货架用查询所有仓库信息
     *
     * @return
     */
    @PostMapping("/selectallmaterial")
    public R selectAllMaterial() {
        List<Map<String,Object>> list = this.warehouseService.selectAllMaterial();
        return new R().add("list", list);
    }

    @PostMapping("/uniSelectAllMaterial")
    public R uniSelectAllMaterial() {
        List<Map<String,Object>> list = this.warehouseService.selectAllMaterial();
        return new R().add("list", list);
    }

    /**
     *查询获取父类菜单
     * @return
     */
    @PostMapping("/materialall")
    public R materialAll() {
        List<Map<String,Object>> list = this.warehouseService.materialall();
        return new R().add("list", list);
    }


    /**
     * 增加仓库区用查询所有仓库信息
     * @return
     */
    @PostMapping("/selectware")
    public R selectWare() {
        List<Warehouse> list = this.warehouseService.list();
        return new R().add("list", list);
    }


    /**
     * 分页查询所有仓库
     *
     * @param page
     * @return
     */
    @PostMapping("/pageselect/{page}")
    public R pageSelect(@PathVariable("page") int page) {
        PageHelper.startPage(page, 5);//第一个参数表示第几页,第二个参数表示每页的条数
        List<Warehouse> list = this.warehouseService.list();
        PageInfo<Warehouse> menuPageInfo = new PageInfo<>(list);
        return new R().add("pageInfo", menuPageInfo);
    }

    /**
     * 添加仓库
     *
     * @param warehouse
     * @return
     */
    @PostMapping("/addware")
    public R addWare(@RequestBody Warehouse warehouse) {
        boolean save = this.warehouseService.save(warehouse);
        return new R().add("save", save);
    }


    /**
     * 修改仓库信息
     *
     * @param warehouse
     * @return
     */
    @PostMapping("/upware")
    public R upWare(@RequestBody Warehouse warehouse) {
        System.out.println(warehouse.getWarehouse());
        QueryWrapper<Warehouse> wareQueryWrapper = new QueryWrapper<>();
        wareQueryWrapper.setEntity(warehouse);
//        boolean update = this.warehouseService.update(wareQueryWrapper);
        boolean update =this.warehouseService.updateById(warehouse);
        return new R().add("update", update);
    }

    /**
     * 根据ID查询该仓库的一条信息
     * @param warehouse
     * @return
     */
    @PostMapping("/seloneid")
    public R selOneid(@RequestBody Warehouse warehouse){
        Warehouse byId = this.warehouseService.getById(warehouse.getId());
        return new R().add("oneid",byId);
    }

    /**
     * 查询所有仓库
     * @return
     */
    @PostMapping("/selectall")
    public R selectAll() {
        List<Warehouse> list = this.warehouseService.list();
        return new R().add("listAll", list);
    }

}
